import { Component, OnInit } from '@angular/core';
import { DatatableComponent } from "@swimlane/ngx-datatable/release";
import { JsonApiService } from "app/core/api/json-api.service";
import { Router, ActivatedRoute, Data } from "@angular/router";

declare var $: any;
@Component({
	selector: 'sa-user',
	templateUrl: './user.component.html',
})
export class UserComponent implements OnInit {
	tableData: any = [];  //table数据
	totalCount: number = 0;	//分页总条数
	curPage: number = 0;    //分页当前页
	pageSize: number = 10;  //分页每页条数
	searchKey: string = '';  //搜索keyword
	workerType: string = '';  //员工类型
	btnData: any = [];
	userAdd: boolean = false;
	userEdit: boolean = false;
	userDeleteBatch: boolean = false;
	userResetPassword: boolean = false;
	userType: string = ''; //用户类型
	userTypeList: any = []; // 用户类型列表
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}
	//初始化钩子函数
	ngOnInit() {
		this.initBtn();
		this.load();
		this.initUserTypeList();
	}
	//刷新
	refresh() { this.load(); }
	//get权限按钮数据
	initBtn() {
		this.btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:user')));
		if (!this.btnData) { return };
		for (let item of this.btnData) {
			if (item.sourceKey === "user:add" && item.parentId) { this.userAdd = true; }
			if (item.sourceKey === "user:edit" && item.parentId) { this.userEdit = true; }
			if (item.sourceKey === "user:deleteBatch" && item.parentId) { this.userDeleteBatch = true; }
			if (item.sourceKey === "user:resetPassword" && item.parentId) { this.userResetPassword = true; }
		}
	}
	//表格搜索
	table_search() {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				name: this.searchKey,
				userType: this.userType,
				workerType: this.workerType,
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("user/list.html", reqdata).subscribe(data => {
			if (data.isSuccess) {
				for (let i = 0; i < data.exhibitDatas.length; i++) {
					data.exhibitDatas[i].serviceEndTimeValue = data.exhibitDatas[i].serviceEndTime ? data.exhibitDatas[i].serviceEndTime.split(' ')[0] : ''
				}
				this.tableData = JSON.parse(JSON.stringify(data.exhibitDatas));
				this.totalCount = data.recordCount;
			}
		})
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	//新建
	new_user() {
		this.router.navigate(['/system/user/user-page'], { queryParams: { type: 'new' } });
	}
	//编辑页面
	updata_user(id) {
		this.router.navigate(['/system/user/user-page'], { queryParams: { id: id, type: 'updata', nowPage: this.curPage } });
	}
	//WEB  API  锁定和解锁
	isLocked(type, row, api, locked) {
		var message = '';
		if (type === 'N') {
			message = "确定锁定" + api + "接口，不让该用户登陆吗？"
		} else if (type === 'Y') {
			message = "确定解除" + api + "接口的锁定吗？</br>解除后用户允许访问" + api + "接口!"
		}
		this.jsonApiService.confirm_message(message).then(() => {
			let reqdata = 'userId=' + row.id + '&locked=' + locked;
			this.jsonApiService.http("user/updateLockedStatus.html", reqdata).subscribe(data => {
				if (data.success) {
					var mess = "";
					if (type === 'N') {
						mess = api + "锁定成功";
					} else if (type === 'Y') {
						mess = api + "解锁成功";
					}
					this.jsonApiService.message(mess);
					this.load();
				} else {
					this.jsonApiService.message(data.message);
				}
			})
		})
	}
	//表格删除用户
	del_user(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("user/deleteBatch.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		});
	}
	//密码重置
	reset_password(row) {
		this.jsonApiService.confirm_message("此操作将重置密码,确认要重置?").then(() => {
			let reqdata = 'id=' + row.id + '&name=' + row.name + '&userName=' + row.userName + '&userType=' + row.userType;
			this.jsonApiService.http("user/resetPassword.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		});
	}
	//菜单权限分配
	menu_access(data) {
		let path = '/system/user';
		common.setCookie('authorizeBackPath', JSON.stringify(path));
		this.router.navigate(['/system/user/authorize'], { queryParams: { id: data.id, type: '菜单', userName: data.userName, nowPage: this.curPage } });
	}
	//车辆权限分配
	car_access(data) {
		let path = '/system/user';
		common.setCookie('authorizeBackPath', JSON.stringify(path));
		this.router.navigate(['/system/user/authorize'], { queryParams: { id: data.id, type: '车辆', userName: data.userName, nowPage: this.curPage } });
	}
	// 获取用户类型列表
	initUserTypeList() {
		this.jsonApiService.http("role/userTypeList.html", {}).subscribe(data => {
			if (data.success) {
				for (let index = 0; index < data.data.length; index++) {
					const element = data.data[index];
					element.text = element.userTypeName
					element.id = element.userType
				}
				this.userTypeList = data.data
			}
		})
	}
}